package cn.imvc.app.models;


import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import cn.imvc.app.entities.Role;
import cn.imvc.app.repositories.RoleRepository;
import cn.imvc.core.ModelUtil;
import cn.imvc.data.domain.ViewRequest;
@Service
public class RoleModelImpl implements RoleModel {

	@Autowired
	private RoleRepository roleRepository;

	public Page<Role> find(ViewRequest viewRequest) {
		return roleRepository.findAll(ModelUtil.OrderByIdDesc(viewRequest));
	}

	public Role get(int id) {
		return roleRepository.findOne(id);
	}

	public Role save(Role role) {
		return roleRepository.saveAndFlush(role);
	}

	public Role update(int id,Role role) {
		Role loadRole=get(id);
		loadRole.setName(role.getName());
		loadRole.setCode(role.getCode());
		loadRole.setRemark(role.getRemark());
		return roleRepository.saveAndFlush(loadRole);
	}

	public void delete(int id) {
		roleRepository.delete(id);
		
	}

	public void deleteBatch(int[] ids) {
		for (int id : ids) {
			delete(id);
		}
		
	}

	public List<Role> find() {
		return roleRepository.findAll(new Sort(Direction.DESC,"order"));
	}
	


}
